package co.yixiang.hw.repository;

import co.yixiang.hw.domain.YxStoreCouponIssue;
import co.yixiang.hw.vo.YxStoreCouponIssueVo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Map;

/**
* @author Walert
* @date 2020-03-10
*/
public interface YxStoreCouponIssueRepository extends JpaRepository<YxStoreCouponIssue, Integer>, JpaSpecificationExecutor<YxStoreCouponIssue> {

    @Query(value = "select                              " +
            "  (select                                  " +
            "       count(1)                            " +
            "   from yx_store_coupon_user tt            " +
            "   where tt.issue_coupon_id = t.id         " +
            "     and tt.use_time = 0                   " +
            "     and tt.`status` = 0                   " +
            "     and tt.is_fail = 0                    " +
            "     and tt.uid = ?1)     as hasCount,     " +
            "  t.id                     as id,          " +
            "  m1.id                    as cid,         " +
            "  t.start_time             as startTime,   " +
            "  t.end_time               as endTime,     " +
            "  t.total_count            as totalCount,  " +
            "  t.remain_count           as remainCount, " +
            "  concat('',m1.type)                  as type,        " +
            "  m1.product_ids           as productIds,  " +
            "  m1.title                 as title,       " +
            "  m1.integral              as integral,    " +
            "  m1.coupon_price          as couponPrice, " +
            "  m1.use_min_price         as useMinPrice, " +
            "  m1.image                 as image,       " +
            "  m1.mark                  as mark         " +
            " from yx_store_coupon_issue t              " +
            " left join yx_store_coupon m1 on (t.cid = m1.id and m1.is_del = 0) " +
            " left join yx_store_coupon_product m2 on (t.cid = m2.cid and m2.is_del = 0) " +
            " where m2.product_id = ?2 " +
            "   and t.is_del = 0 " +
            "   and t.`status` = 1 " +
            "   and t.start_time <= ?3 " +
            "   and t.end_time >= ?3 ",nativeQuery = true)
    List<Map> getIssueCouponList(Integer uid, Integer goodsId, Integer time);
}